﻿using UnityEngine;
using System.Collections;
using System;

public class Utl : MonoBehaviour
{
    public static Utl instance;

    public GameObject audioPre;

    void Awake()
    {
        instance = this;
    }

    public Coroutine WaitForSec(float sec, Action cb)
    {
       return StartCoroutine(WaitForSec1(sec, cb));
    }

    IEnumerator WaitForSec1(float sec, Action cb)
    {
        yield return new WaitForSeconds(sec);
        cb();
    }

    public void WaitForFrame(int num, Action cb)
    {
        StartCoroutine(WaitForFrame1(num, cb));
    }

    IEnumerator WaitForFrame1(int num, Action cb)
    {
        for (int i = 0; i < num; i++)
        {
            yield return new WaitForEndOfFrame();
        }
        cb();
    }

    public void WaitForLoop(int sec, Action<int> cbp)
    {
        StartCoroutine(WaitForLoop1(sec, cbp));
    }

    IEnumerator WaitForLoop1(int sec, Action<int> cbp)
    {
        for (int i = 0; i < sec; i++)
        {
            cbp(sec - i);
            yield return new WaitForSeconds(1);
        }
        cbp(0);
    }

    public GameObject PlaySound(string clipName, bool isLoop, float volume, bool is3D, float maxDis, Vector3 pos, Transform parent = null)
    {
        GameObject go = Instantiate(audioPre, pos, Quaternion.identity) as GameObject;
        AudioCtrl ac = go.GetComponent<AudioCtrl>();
        ac.SetInfo(clipName, isLoop, volume, is3D, maxDis);
        go.transform.parent = parent;
        return go;
    }

    public void SetLayer(string layerName, GameObject target)
    {
        target.layer = LayerMask.NameToLayer(layerName);
        for (int i = 0; i < target.transform.childCount; i++)
        {
            SetLayer(layerName, target.transform.GetChild(i).gameObject);
        }
    }

    public String ChangDis(float dis) 
    { 
        
        if(0<dis&&dis<10)
        {
            return (int)(dis * 100 )+ "m";
        }
        else
        {
            return (int)dis / 10 + "km";
        }
    }
}
